	$(document).ready(function () {
		 loadTable();
		 
	})		
		
		
	var proDate;
	function  loadTable()
	{	
	    var url='/budgetMonth/getPro';
		var result = common_ajax.ajaxFunc(url,null,"json");
		proDate=result;
		var columnsAll = new Array();
		columnsAll.push( {field:'month',title:'月份',align:'center',width:100,rowspan:3});
		columnsAll.push( {field:'lc',title:'疗程',align:'center',colspan:5});
		for(var n=0;n<result.length;n++)
		{
			columnsAll.push({field:'pro_'+result[n].ids,title:result[n].name,align:'center',colspan:15});
		}
	
		var columnsAll1 = new Array();
		columnsAll1.push( {field:'lc_newcus',title:'新增顾客',align:'center',colspan:5});
		for(var i=0;i<result.length;i++)
		{
			columnsAll1.push({field:'pro_'+result[i].ids+'_jhnum',title:'计划数量',align:'center',colspan:3});
			columnsAll1.push({field:'pro_'+result[i].ids+'_jhprice',title:'计划金额',align:'center',colspan:3});
			columnsAll1.push({field:'pro_'+result[i].ids+'_sjnum',title:'实际数量',align:'center',colspan:3});
			columnsAll1.push({field:'pro_'+result[i].ids+'_sjprice',title:'实际金额',align:'center',colspan:3});
			columnsAll1.push({field:'pro_'+result[i].ids+'_wcbl',title:'完成比率',align:'center',colspan:3});
		}
		
		var columnsAll2 = new Array();
		columnsAll2.push({field:'lc_newcus_jhnum',title:'计划个数',align:'center',width:60,editor:{type:'numberspinner'},
			formatter: function(value,row,index)
			{
				var lc_newcus_jhnum=row.lc_newcus_jhnum;
				if(lc_newcus_jhnum!=null){return row.lc_newcus_jhnum=lc_newcus_jhnum}
				else{return row.lc_newcus_jhnum="0"}
			}
		});
		columnsAll2.push({field:'lc_newcus_jhlcnum',title:'计划疗程数',align:'center',width:60,editor:{type:'numberspinner'},
			formatter: function(value,row,index)
			{
				var lc_newcus_jhlcnum=row.lc_newcus_jhlcnum;
				if(lc_newcus_jhlcnum!=null){return row.lc_newcus_jhlcnum=lc_newcus_jhlcnum}
				else{return row.lc_newcus_jhlcnum="0"}
			}
		});
		columnsAll2.push({field:'lc_newcus_sjnum',title:'实际个数',align:'center',width:60,
			formatter: function(value,row,index)
			{
				var lc_newcus_sjnum=row.lc_newcus_sjnum;
				if(lc_newcus_sjnum!=null){return row.lc_newcus_sjnum=lc_newcus_sjnum}
				else{return row.lc_newcus_sjnum="0"}
			}
		});
		columnsAll2.push({field:'lc_newcus_sjlcnum',title:'实际疗程数',align:'center',width:60,
			formatter: function(value,row,index)
			{
				var lc_newcus_sjlcnum=row.lc_newcus_sjlcnum;
				if(lc_newcus_sjlcnum!=null){return row.lc_newcus_sjlcnum=lc_newcus_sjlcnum}
				else{return row.lc_newcus_sjlcnum="0"}
			}
		});
		columnsAll2.push({field:'lc_newcus_wcbl',title:'完成比率',align:'center',width:60,
			formatter: function(value,row,index)
			{
				var lc_newcus_jhnum=row.lc_newcus_jhnum;
				var lc_newcus_sjnum=row.lc_newcus_sjnum;
				if(lc_newcus_jhnum!="0")
				{
					var  bl=parseFloat(lc_newcus_sjnum)/parseFloat(lc_newcus_jhnum)*100;
					var  lc_newcus_wcbl=bl+"%";
					return  row.lc_newcus_wcbl=lc_newcus_wcbl;
				}
				else
				{
					return row.lc_newcus_wcbl="0%"
				}
			}
		});
		for(var m=0;m<result.length;m++)
		{
			columnsAll2.push({field:'pro_'+result[m].ids+'_jhnum_newcus',title:'新顾客',align:'center',width:60,editor:{type:'numberspinner'}});
			columnsAll2.push({field:'pro_'+result[m].ids+'_jhnum_oldcus',title:'老顾客',align:'center',width:60,editor:{type:'numberspinner'}});
			columnsAll2.push({field:'pro_'+result[m].ids+'_jhnum_hj',title:'合计',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_jhprice_newcus',title:'新顾客',align:'center',width:60,editor:{type:'numberspinner'}});
			columnsAll2.push({field:'pro_'+result[m].ids+'_jhprice_oldcus',title:'老顾客',align:'center',width:60,editor:{type:'numberspinner'}});
			columnsAll2.push({field:'pro_'+result[m].ids+'_jhprice_hj',title:'合计',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_sjnum_newcus',title:'新顾客',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_sjnum_oldcus',title:'老顾客',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_sjnum_hj',title:'合计',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_sjprice_newcus',title:'新顾客',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_sjprice_oldcus',title:'老顾客',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_sjprice_hj',title:'合计',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_wcbl_newcus',title:'新顾客',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_wcbl_oldcus',title:'老顾客',align:'center',width:60});
			columnsAll2.push({field:'pro_'+result[m].ids+'_wcbl_hj',title:'合计',align:'center',width:60});
		}
		
		
		var columns=[columnsAll,columnsAll1,columnsAll2];
		
		$('#monthBudget_datagrid').datagrid({
			  iconCls: 'icon-edit',
			  singleSelect:true,  
			  columns:columns,
			  toolbar: '#tb',
			  url:'budgetMonth/getBudgetMonth',
			  onClickRow: onClickRow,
			  onLoadSuccess:function(data){
				  sumhj();
			  }
		});
		
	}
	function   sumhj()
	{
		var rows = $('#monthBudget_datagrid').datagrid('getRows');
		for(var i=0;i<rows.length;i++)
		{	
			var jhnum_hj=0;
			var jhprice_hj=0;
			var sjnum_hj=0;
			var sjprice_hj=0;
			var wcbl_newcus=0;
			var wcbl_oldcus=0;
			var wcbl_hj=0;
			for(var j=0;j<proDate.length;j++)
			{	
				var ids=proDate[j].ids;
				
				var jhnum_newcus=rows[i]['pro_'+ids+'_jhnum_newcus'];
				var jhnum_oldcus=rows[i]['pro_'+ids+'_jhnum_oldcus'];
				jhnum_hj=parseFloat(jhnum_newcus)+parseFloat(jhnum_oldcus);
				
				var jhprice_newcus=rows[i]['pro_'+ids+'_jhprice_newcus'];
				var jhprice_oldcus=rows[i]['pro_'+ids+'_jhprice_oldcus'];
				jhprice_hj=parseFloat(jhprice_newcus)+parseFloat(jhprice_oldcus);
				
				var sjnum_newcus=rows[i]['pro_'+ids+'_sjnum_newcus'];
				var sjnum_oldcus=rows[i]['pro_'+ids+'_sjnum_oldcus'];
				sjnum_hj=parseFloat(sjnum_newcus)+parseFloat(sjnum_oldcus);
				
				var sjprice_newcus=rows[i]['pro_'+ids+'_sjprice_newcus'];
				var sjprice_oldcus=rows[i]['pro_'+ids+'_sjprice_oldcus'];
				sjprice_hj=parseFloat(sjprice_newcus)+parseFloat(sjprice_oldcus);
				
				var wcblnewcus=0;
				if(jhnum_newcus!="0")
				{
					var wcbln=parseFloat(sjnum_newcus)/parseFloat(jhnum_newcus)*100;
					wcblnewcus=wcbln;
				}
				wcbl_newcus=wcblnewcus+"%";
				
				var wcbloldcus=0;
				if(jhnum_oldcus!="0")
				{
					var wcblo=parseFloat(sjnum_newcus)/parseFloat(jhnum_newcus)*100;
					wcbloldcus=wcblo;
				}
				wcbl_oldcus=wcbloldcus+"%";
				
				var wcblhj=wcblnewcus+wcbloldcus;
				wcbl_hj=wcblhj+"%";
				
				if (rows[i]) {
					
		            var n=$('#monthBudget_datagrid').datagrid('getRowIndex',rows[i]);
		            
		            var pro_jhnum_hj=$('.datagrid-body td[field="pro_'+ids+'_jhnum_hj"]')[n];
		            var div = $(pro_jhnum_hj).find('div')[0];
		            $(div).text(jhnum_hj);
		            
		            var pro_jhprice_hj=$('.datagrid-body td[field="pro_'+ids+'_jhprice_hj"]')[n];
		            var div = $(pro_jhprice_hj).find('div')[0];
		            $(div).text(jhprice_hj);
		            
		            var pro_sjnum_hj=$('.datagrid-body td[field="pro_'+ids+'_sjnum_hj"]')[n];
		            var div = $(pro_sjnum_hj).find('div')[0];
		            $(div).text(sjnum_hj);
		            
		            var pro_sjprice_hj=$('.datagrid-body td[field="pro_'+ids+'_sjprice_hj"]')[n];
		            var div = $(pro_sjprice_hj).find('div')[0];
		            $(div).text(sjprice_hj);
		            
		            var pro_wcbl_newcus=$('.datagrid-body td[field="pro_'+ids+'_wcbl_newcus"]')[n];
		            var div = $(pro_wcbl_newcus).find('div')[0];
		            $(div).text(wcbl_newcus);
		            
		            var pro_wcbl_oldcus=$('.datagrid-body td[field="pro_'+ids+'_wcbl_oldcus"]')[n];
		            var div = $(pro_wcbl_oldcus).find('div')[0];
		            $(div).text(wcbl_oldcus);
		            
		            var pro_wcbl_hj=$('.datagrid-body td[field="pro_'+ids+'_wcbl_hj"]')[n];
		            var div = $(pro_wcbl_hj).find('div')[0];
		            $(div).text(wcbl_hj);
		        }
			}
		}
		
	}
	
	
	
	function  save()
	{
		endEditing();
		var rows = $('#monthBudget_datagrid').datagrid('getRows')[0];
		var rowjson=JSON.stringify(rows);
		var url='/budgetMonth/saveBudget';
		var data={"budgetJson":rowjson};
		var reData = common_ajax.ajaxFunc(url,data);
		if(reData == "200")
		{
			 $.slideMsg("保存成功", parent.$);
//			 parent.refresh();
		}else
		{
			$.alert(parent.$, reData);
		}
	}
	
	function  quding()
	{	
		endEditing();
		sumhj();
		
	}
	
	
	/*数据表格行编辑*/
	var editIndex1 = undefined;
	function endEditing(){
		if (editIndex1 == undefined){return true}
		if ($('#monthBudget_datagrid').datagrid('validateRow', editIndex1)){
			$('#monthBudget_datagrid').datagrid('endEdit', editIndex1);
				editIndex1 = undefined;
				return true;
		} else {
			return false;
		}
	}
	function onClickRow(index){
		if(index==0)
		{
			if (editIndex1 != index){
				if (endEditing()){
					$('#monthBudget_datagrid').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
						editIndex1 = index;
				} else {
					$('#monthBudget_datagrid').datagrid('selectRow', editIndex1);
				}
			}
		}
		
	}